#!/bin/sh

daemon=false
appname=bpmdemo
jarfile=target/${appname}.jar
[ ! -e "$jarfile" ] && jarfile=${appname}.jar
Survivor=2 Old=64 NewSize=$[Survivor*10] Xmx=$[NewSize+Old] #NewSize=Survivor*(1+1+8) Xmx=NewSize+Old
JVM_OPS="-Djava.compiler=none -Xmx${Xmx}m -Xms${Xmx}m -XX:NewSize=${NewSize}m -XX:MaxNewSize=${NewSize}m -XX:SurvivorRatio=8 -Xss228k"
#JVM_OPS="$JVM_OPS -Dlogserver -DcontextName=bpmdemo"
#JVM_OPS="$JVM_OPS -Dserver.port=8086 -Dserver.ssl.enabled=false -Dmysql.host=xlongwei"
#JVM_OPS="$JVM_OPS -Dspring.redis.host=xlongwei -Dab.cache.type=redis -Dab.simple-mq.message-queue-type=redis"
#JVM_OPS="$JVM_OPS -Dswagger.host=bpm.xlongwei.com -Dspring.main.banner-mode=OFF"
#JVM_OPS="$JVM_OPS -Dmail.enabled=true -Dab.mail.mail-address=notify@xlongwei.com -Dab.mail.nick-name=notify -Dab.mail.send-host=smtp.exmail.qq.com -Dab.mail.password="
JVM_OPS="$JVM_OPS -Duser.timezone=GMT+8"
#JVM_OPS="$JVM_OPS -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"
#ENV_OPS="$ENV_OPS PATH=/usr/java/jdk1.8.0_161/bin:$PATH"
#profile="-P mysql5"

usage(){
    echo "Usage: start.sh ( commands ... )"
    echo "commands: "
    echo "  status      check the running status"
    echo "  start       start $appname"
    echo "  stop        stop $appname"
    echo "  restart     stop && start"
    echo "  clean       clean target"
    echo "  jar         build $jarfile"
    echo "  jars        copy dependencies to target"
    echo "  package     jar && jars"
    echo "  rebuild     stop && jar && start"
    echo "  refresh     stop && clean && jar && jars && start"
    echo "  deploy      package fat-jar $jarfile"
    echo "  redeploy    package fat-jar $jarfile and restart"
    echo "  keystore    prepare keystore.p12"
}

status(){
    PIDS=`ps -ef | grep java | grep "$jarfile" |awk '{print $2}'`

	if [ -z "$PIDS" ]; then
	    echo "$appname is not running!"
	else
		for PID in $PIDS ; do
		    echo "$appname has pid: $PID!"
		done
	fi
}

stop(){
    PIDS=`ps -ef | grep java | grep "$jarfile" |awk '{print $2}'`

	if [ -z "$PIDS" ]; then
	    echo "$appname is not running!"
	else
		echo -e "Stopping $appname ..."
		for PID in $PIDS ; do
			echo -e "kill $PID"
		    kill $PID > /dev/null 2>&1
		done
	fi
}

clean(){
	mvn clean
}

jar(){
	mvn $profile compile jar:jar
}

jars(){
	mvn $profile dependency:copy-dependencies -DoutputDirectory=target
}

deploy(){
	mvn package -Prelease $profile -Dmaven.test.skip=true -Dmaven.javadoc.skip=true
}

start(){
	echo "starting $appname ..."
	JVM_OPS="-server -Djava.awt.headless=true $JVM_OPS"
	if [ "$daemon" = "true" ]; then
		env $ENV_OPS setsid java $JVM_OPS -jar $jarfile >> /dev/null 2>&1 &
	else
		env $ENV_OPS java $JVM_OPS -jar $jarfile 2>&1
	fi
}

keystore(){
	keystore=src/main/resources/keystore.p12
	if [ ! -e ${keystore} ]; then
		if [ $# -gt 1 ]; then 
			keytool -genkey -alias server -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore ${keystore} -dname "CN=localhost, OU=localhost, O=localhost, L=Chengdu, ST=Sichuan, C=CN"
		else
		    cert=/soft/cert
			openssl pkcs12 -export -in $cert/xlongwei.pem -inkey $cert/xlongwei.key -name server -out ${keystore}
		fi
	else
		keytool -list -keystore ${keystore} -v -storepass password
	fi
}

if [ $# -eq 0 ]; then 
    usage
else
	case $1 in
	status) status ;;
	start) start ;;
	stop) stop ;;
	clean) clean ;;
	jar) jar ;;
	jars) jars ;;
	package) jar && jars ;;
	restart) stop && start ;;
	rebuild) stop && jar && start ;;
	refresh) stop && clean && jar && jars && start ;;
	deploy) deploy ;;
	redeploy) stop && deploy && start ;;
	keystore) keystore $@;;
	*) usage ;;
	esac
fi
